﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NHTU;
using System.Threading;

namespace NOMFinalSettlement.AppForm.App
{
    public partial class fBackup : DevComponents.DotNetBar.Office2007Form
    {
        NOMFinalSettlement.Classes.BackupDatabase _bak = new NOMFinalSettlement.Classes.BackupDatabase();
        string currentFile = "";

        public fBackup()
        {
            InitializeComponent();
        }

        private void fBackup_Load(object sender, EventArgs e)
        {
            _reLoadSetting();
            _bak.ListBackupFile(listViewFolder);
        }
   
        private void btnBackup_Click(object sender, EventArgs e)
        {
            string backupDir = txtBakPath.Text;
            if (backupDir != "")
            {
                try
                {
                    //Thread threadBackup = new Thread(new ThreadStart(doBackup));
                    //threadBackup.Start();
                    Thread threadBackup = new Thread(delegate()
                    {
                        doBackup();
                    });
                    threadBackup.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MsgBox.Show("Vui lòng thiết lập đường dẫn sao lưu.", "Thông báo", MsgBoxButton.Ok, MsgBoxIcon.Warning);
            }
        }

        private void btnBakPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDlg = new FolderBrowserDialog();
            folderDlg.ShowNewFolderButton = true;
            // Show the FolderBrowserDialog.
            DialogResult result = folderDlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtBakPath.Text = folderDlg.SelectedPath;
                _bak.BackUpDir = folderDlg.SelectedPath;
            }
        }

        private void btnSaveSetting_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtBakPath.Text))
            {
                _saveSetting();
                _reLoadSetting();
                _bak.ListBackupFile(listViewFolder);
                MsgBox.Show("Thiết lập thông tin sao lưu dữ liệu thành công.", "Sao lưu dữ liệu", MsgBoxButton.Ok, MsgBoxIcon.Success);
            }
            else
            {
                MsgBox.Show("Vui lòng thiết lập thư mục lưu trữ.","Cảnh báo", MsgBoxButton.Ok,MsgBoxIcon.Warning);
            }
        }

        void _reLoadSetting()
        {
            _bak.LoadSetting();
            txtBakPath.Text = _bak.BackUpDir;
        }
        void _saveSetting()
        {
            _bak.BackUpDir = txtBakPath.Text;
            _bak.SaveSetting();
        }

        //backup
        void doBackup()
        {
            Properties.Settings.Default.Reload();

            BackupInvoke(false, Cursors.WaitCursor);
            string filePath = MainApp.Self.appProcess.fileGeneral(txtBakPath.Text);
            SQLServer.ServerName = MainApp.Self.connInfo.DataSource;
            SQLServer.DatabaseName = MainApp.Self.connInfo.InitialCatalog;
            SQLServer.Username = MainApp.Self.connInfo.UserID;
            SQLServer.Password = MainApp.Self.connInfo.Password;

            SQLServer.FilePath = filePath;
            SQLServer.ProgressBar = pBar;
            SQLServer.StatusLabel = lbStatus;
            bool bak = SQLServer.Backup(this);
            if (bak)
            {
                MsgBox.Show("Sao lưu dữ liệu thành công.", "Sao lưu dữ liệu", MsgBoxButton.Ok, MsgBoxIcon.Success);
                this.Invoke(new MethodInvoker(() => _bak.ListBackupFile(listViewFolder)), null);
            }
            else
            {
                MsgBox.Show("Sao lưu dữ liệu không thành công.\n" + SQLServer.Message, "Lỗi xử lý", MsgBoxButton.Ok, MsgBoxIcon.Warning);
            }
            BackupInvoke(true, Cursors.Default);
        }


        
        void BackupInvoke(bool yes, Cursor c)
        {
            this.Invoke((MethodInvoker)delegate {
                btnBackup.Enabled = yes;
                btnBakPath.Enabled = yes;
                btnSaveSetting.Enabled = yes;
                this.Cursor = c;
            });
        }

        private void menuDel_Click(object sender, EventArgs e)
        {
            if (_bak.DeleteBackup(this.currentFile))
            {
                MsgBox.Show("Đã xóa thành công dữ liệu: " + this.currentFile, "Thông báo", MsgBoxButton.Ok, MsgBoxIcon.Success);
                _bak.LoadSetting();
                _bak.ListBackupFile(listViewFolder);
            }
        }

        private void menuDelAll_Click(object sender, EventArgs e)
        {
            if (MsgBoxResult.Yes != MsgBox.Show(
            "Dữ liệu này rất quan trọng, bạn có muốn xóa hết?",
            "Lưu ý:",
             MsgBoxButton.YesNo,
             MsgBoxIcon.Question))
            {
                return;
            }

            _bak.DeleteAllBackUp();
            _bak.LoadSetting();
            _bak.ListBackupFile(listViewFolder);
        }

        private void menuRefresh_Click(object sender, EventArgs e)
        {
            _bak.LoadSetting();
            _bak.ListBackupFile(listViewFolder);
        }

        private void menuBackup_Click(object sender, EventArgs e)
        {
            string backupDir = txtBakPath.Text;
            if (backupDir != "")
            {
                try
                {
                    //Thread threadBackup = new Thread(new ThreadStart(doBackup));
                    //threadBackup.Start();
                    Thread threadBackup = new Thread(delegate()
                    {
                        doBackup();
                    });
                    threadBackup.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MsgBox.Show("Vui lòng thiết lập đường dẫn sao lưu.", "Thông báo", MsgBoxButton.Ok, MsgBoxIcon.Warning);
            }
        }

        private void listViewFolder_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.currentFile = _bak.GetItemListView(listViewFolder);
        }

        private void listViewFolder_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (listViewFolder.FocusedItem.Bounds.Contains(e.Location) == true)
                {
                    ctxMenu.Show(Cursor.Position);
                }
            }
        }

    }
}
